PyIgnition

https://github.com/animatinator/PyIgnition update for Python 3
Clone: git clone https://git.frombelow.net/PyIgnition.git
Log | Files | Refs | README

Controlled Eruption.py (7419B)


      1 # Controlled Eruption - PyIgnition beta 1 demo
      2 # Copyright David Barker 2010
      3 #
      4 # Be forewarned - this is quite possibly the messiest code you will see all year.
      5 # Let this mess be a lesson to those who try tostart coding without deciding
      6 # what they're going to code beforehand...
      7 
      8 import PyIgnition, pygame, sys, math, random
      9 
     10 
     11 pygame.font.init()
     12 screen = pygame.display.set_mode((800, 600))
     13 pygame.display.set_caption("PyIgnition 'Controlled Eruption' demo")
     14 clock = pygame.time.Clock()
     15 
     16 curframe = 0
     17 started = False
     18 
     19 # 'Press space to start' text
     20 starttextfont = pygame.font.Font("courbd.ttf", 50)
     21 starttext = starttextfont.render("Press space to start", True, (255, 255, 255), (0, 0, 0))
     22 starttextpos = ((400 - (starttext.get_width() / 2)), (300 - (starttext.get_height() / 2)))
     23 
     24 # Background
     25 background = PyIgnition.ParticleEffect(screen, (0, 0), (800, 600))
     26 backgroundsource = background.CreateSource((10, 10), initspeed = 5.0, initdirection = 2.35619449, initspeedrandrange = 2.0, initdirectionrandrange = 1.0, particlesperframe = 5, particlelife = 125, drawtype = PyIgnition.DRAWTYPE_SCALELINE, colour = (255, 255, 255), length = 10.0)
     27 backgroundsource.CreateParticleKeyframe(50, colour = (0, 255, 0), length = 10.0)
     28 backgroundsource.CreateParticleKeyframe(75, colour = (255, 255, 0), length = 10.0)
     29 backgroundsource.CreateParticleKeyframe(100, colour = (0, 255, 255), length = 10.0)
     30 backgroundsource.CreateParticleKeyframe(125, colour = (0, 0, 0), length = 10.0)
     31 backgroundsource2 = background.CreateSource((790, 10), initspeed = 5.0, initdirection = -2.35619449, initspeedrandrange = 2.0, initdirectionrandrange = 1.0, particlesperframe = 0, particlelife = 125, drawtype = PyIgnition.DRAWTYPE_SCALELINE, colour = (255, 255, 255), length = 10.0)
     32 backgroundsource2.CreateParticleKeyframe(50, colour = (0, 255, 0), length = 10.0)
     33 backgroundsource2.CreateParticleKeyframe(75, colour = (255, 255, 0), length = 10.0)
     34 backgroundsource2.CreateParticleKeyframe(100, colour = (0, 255, 255), length = 10.0)
     35 backgroundsource2.CreateParticleKeyframe(125, colour = (0, 0, 0), length = 10.0)
     36 
     37 # Periodic firework
     38 fireworkcounter = 0.0
     39 fireworkdist = 200.0
     40 firework = PyIgnition.ParticleEffect(screen, (0, 0), (800, 600))
     41 firework.CreateDirectedGravity(strength = 0.2, direction = [0, 1])
     42 fireworksource = firework.CreateSource((10, 10), initspeed = 8.0, initdirection = 0.0, initspeedrandrange = 2.0, initdirectionrandrange = math.pi, particlesperframe = 0, particlelife = 150, drawtype = PyIgnition.DRAWTYPE_IMAGE, imagepath = "Spark.png")
     43 fireworkblast = background.CreateCircle(pos = (1000, 1000), colour = (0, 0, 0), bounce = 1.5, radius = 100.0)
     44 
     45 # Ground-level bubbles
     46 bubbles = PyIgnition.ParticleEffect(screen, (0, 0), (800, 600))
     47 bubblesource = bubbles.CreateSource(initspeed = 1.0, initdirection = 0.0, initspeedrandrange = 0.5, initdirectionrandrange = math.pi, particlesperframe = 0, particlelife = 200, colour = (200, 255, 200), drawtype = PyIgnition.DRAWTYPE_BUBBLE, radius = 5.0, genspacing = 5)
     48 bubblesource.CreateParticleKeyframe(500, colour = (250, 100, 250))
     49 bubblesource.CreateParticleKeyframe(75, colour = (190, 190, 200))
     50 bubblesource.CreateParticleKeyframe(100, colour = (50, 250, 252))
     51 bubblesource.CreateParticleKeyframe(125, colour = (250, 250, 255))
     52 bubbles.CreateDirectedGravity(strength = 0.04, direction = [0, -1])
     53 
     54 # Fire, just for laughs
     55 fire = PyIgnition.ParticleEffect(screen, (0, 0), (800, 600))
     56 gravity = fire.CreateDirectedGravity(strength = 0.07, direction = [0, -1])
     57 wind = fire.CreateDirectedGravity(strength = 0.05, direction = [1, 0])
     58 source = fire.CreateSource((150, 500), initspeed = 2.0, initdirection = 0.0, initspeedrandrange = 1.0, initdirectionrandrange = 0.5, particlesperframe = 10, particlelife = 100, drawtype = PyIgnition.DRAWTYPE_CIRCLE, colour = (255, 200, 100), radius = 3.0)
     59 source.CreateParticleKeyframe(10, colour = (200, 50, 20), radius = 4.0)
     60 source.CreateParticleKeyframe(30, colour = (150, 0, 0), radius = 6.0)
     61 source.CreateParticleKeyframe(60, colour = (50, 20, 20), radius = 20.0)
     62 source.CreateParticleKeyframe(80, colour = (0, 0, 0), radius = 50.0)
     63 
     64 # Text
     65 font = pygame.font.Font("euphemia.ttf", 70)
     66 font2 = pygame.font.Font("euphemia.ttf", 40)
     67 text = font.render("PyIgnition", True, (255, 255, 255), (0, 0, 0))
     68 text2 = font2.render("ExeSoft", True, (200, 200, 200), (0, 0, 0))
     69 textalpha = font.render("PyIgnition", True, (255, 255, 255))
     70 text2alpha = font2.render("ExeSoft", True, (200, 200, 200))
     71 temptext = text.copy()
     72 temptext2 = text2.copy()
     73 temptext.set_alpha(0)
     74 temptext2.set_alpha(0)
     75 textpos = ((400 - (text.get_width() / 2)), 250)
     76 textpos2 = (textpos[0] + 110, textpos[1] - 30)
     77 font3 = pygame.font.Font("courbd.ttf", 20)
     78 text3 = font3.render("Version 1.0", True, (200, 200, 255), (0, 0, 0))
     79 textpos3 = ((800 - text3.get_width()) - 5, (600 - text3.get_height()))
     80 
     81 
     82 def Update():
     83     global curframe, fireworkcounter, temptext, temptext2
     84     
     85     background.Update()
     86 
     87     if curframe == 100:
     88         backgroundsource2.SetParticlesPerFrame(5)
     89 
     90     fireworksource.SetPos((400 + fireworkdist * math.cos(fireworkcounter), 300 + fireworkdist * math.sin(fireworkcounter)))
     91     if (curframe > 200) and (curframe % 50 == 0):
     92         fireworksource.CreateKeyframe(fireworksource.curframe, particlesperframe = 10)
     93         fireworksource.CreateKeyframe(fireworksource.curframe + 4, particlesperframe = 0)
     94         firework.Update()
     95         fireworkblast.SetPos(fireworksource.pos)
     96         fireworksource.ConsolidateKeyframes()
     97         #fireworkblast.ConsolidateKeyframes()
     98     else:
     99         if curframe % 30 == 0:
    100             fireworkblast.ConsolidateKeyframes()
    101         firework.Update()
    102         fireworkblast.SetPos((1000, 1000))
    103     fireworkcounter = fireworkcounter + 0.1
    104 
    105     random.seed()
    106     if curframe == 400:
    107         bubblesource.SetParticlesPerFrame(1)
    108     bubbles.Update()
    109     bubblesource.SetPos((random.randint(0, 800), 600))
    110     if curframe % 30 == 0:
    111         bubblesource.ConsolidateKeyframes()
    112 
    113     if curframe > 500:
    114         fire.Update()
    115         source.SetPos(pygame.mouse.get_pos())
    116         if curframe % 30 == 0:
    117             source.ConsolidateKeyframes()
    118 
    119     if curframe > 400:
    120         if curframe > 500:
    121             temptext = textalpha.copy()
    122             temptext2 = text2alpha.copy()
    123         else:
    124             factor = (float(curframe) - 400.0) / 100.0
    125             if factor > 1.0:
    126                 factor = 1.0
    127             alpha = int(factor * 255.0)
    128             temptext = text.copy()
    129             temptext.set_alpha(alpha)
    130             temptext2 = text2.copy()
    131             temptext2.set_alpha(alpha)
    132 
    133     curframe = curframe + 1
    134 
    135 def Redraw():
    136     if curframe > 500:
    137         screen.blit(text3, textpos3)
    138     fire.Redraw()
    139     screen.blit(temptext, textpos)
    140     screen.blit(temptext2, textpos2)
    141     background.Redraw()
    142     firework.Redraw()
    143     bubbles.Redraw()
    144 
    145 
    146 while True:
    147     for event in pygame.event.get():
    148         if event.type == pygame.QUIT:
    149             sys.exit()
    150         elif event.type == pygame.KEYDOWN:
    151             if event.key == pygame.K_SPACE:
    152                 started = True
    153 
    154     screen.fill((0, 0, 0))
    155 
    156     if started:
    157         Update()
    158         Redraw()
    159     else:
    160         screen.blit(starttext, starttextpos)
    161 
    162     pygame.display.update()
    163     clock.tick(30)